﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProgNetComponents.Design.Attributes;
using DevExpress.XtraEditors;
using System.ComponentModel;
using ProgNetComponents.Design.Editors;
using DevExpress.XtraEditors.Repository;
using System.Collections;
using System.Collections.ObjectModel;
using DevExpress.Utils.Editors;
using System.Drawing.Design;
using ProgNetComponentsDx.Language.Design.Editors;
using ProgNetComponentsDx.Language.Design.TypeConverters;
using ProgNetComponentsDx.Base.Classes;

namespace ProgNetComponentsDx.Language
{
    public class LanguageSettings : BaseSettings
    {
        public LanguageSettings(object target, LanguageProvider provider)
            : base(target, provider)
        {
            _RadioItemLngSettings = new RadioItemLngSettingsCollection(target);
            _RadioItemLngSettings.CollectionCahnged += new EventHandler(_RadioItemLngSettings_CollectionCahnged);
        }

        #region Is Browsable methods
        private bool IsNotForRadio()
        {
            return (SetTextFromDialogResult == false)
                && !(Owner is RadioGroup ||
                Owner is RepositoryItemRadioGroup);
        }
        #endregion
        void _RadioItemLngSettings_CollectionCahnged(object sender, EventArgs e)
        {
            OnSettingsChanged();
        }
        
        private RadioItemLngSettingsCollection _RadioItemLngSettings = null;

        private string _StringCode;
        private string _StringDictionary;
        private string _Prefix;
        private string _Surfix;
        private bool _SetTextFromDialogResult = false;

        [BrowsableByMethod("IsNotForRadio")]
        [UIEditor(typeof(LngStringEditor))]
        [System.ComponentModel.NotifyParentProperty(true)]
        [AlwaysSerialize]
        public string StringCode
        {
            get { return _StringCode; }
            set
            {
                if (_StringCode != value)
                {
                    _StringCode = value;
                    OnSettingsChanged();
                }
            }
        }

        [BrowsableByMethod("IsNotForRadio")]
        [System.ComponentModel.NotifyParentProperty(true)]
        public string StringDictionary
        {
            get { return _StringDictionary; }
            set
            {
                if (_StringDictionary != value)
                {
                    _StringDictionary = value;
                    OnSettingsChanged();
                }
            }
        }

        [BrowsableByMethod("IsNotForRadio")]
        [System.ComponentModel.NotifyParentProperty(true)]
        public string Prefix
        {
            get { return _Prefix; }
            set
            {
                if (_Prefix != value)
                {
                    _Prefix = value;
                    OnSettingsChanged();
                }
            }
        }
        
        [BrowsableByPropertyValue("SetTextFromDialogResult", false)]
        [HiddenByPropertyValueType("Owner", typeof(RadioGroup), typeof(RepositoryItemRadioGroup))]
        [System.ComponentModel.NotifyParentProperty(true)]
        public string Surfix
        {
            get { return _Surfix; }
            set
            {
                if (_Surfix != value)
                {
                    _Surfix = value;
                    OnSettingsChanged();
                }
            }
        }

        [BrowsableByPropertyValueType("Owner", typeof(SimpleButton))]
        [RefreshProperties(RefreshProperties.All)]
        [System.ComponentModel.NotifyParentProperty(true)]
        public bool SetTextFromDialogResult
        {
            get { return _SetTextFromDialogResult; }
            set
            {
                if (_SetTextFromDialogResult != value)
                {
                    _SetTextFromDialogResult = value;
                    OnSettingsChanged();
                }
            }
        }

        [BrowsableByPropertyValueType("Owner", typeof(RadioGroup), typeof(RepositoryItemRadioGroup))]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [RefreshProperties(RefreshProperties.All)]
        [System.ComponentModel.NotifyParentProperty(true)]
        public RadioItemLngSettingsCollection RadioItemLngSettings
        {
            get { return _RadioItemLngSettings; }
        }
    }
    [TypeConverter(typeof(RadioItemLngSettingsConverter))]
    public class RadioItemLngSettings
    {
        internal object Owner
        {
            get;
            set;
        }
        public RadioItemLngSettings() { }
        public RadioItemLngSettings(object ItemValue, string StringCode)
        {
            this.ItemValue = ItemValue;
            this.StringCode = StringCode;
        }

        [Editor(typeof(RadioItemValueEditor), typeof(UITypeEditor))]
        [System.ComponentModel.NotifyParentProperty(true)]
        public object ItemValue
        {
            get;
            set;
        }
        [System.ComponentModel.NotifyParentProperty(true)]
        public string StringCode
        {
            get;
            set;
        }

        public override string ToString()
        {
            return ItemValue.AsString() + " [" + StringCode + "]";
        }
    }

    [Editor(typeof(RadioItemLngSettingsCollectionEditor), typeof(UITypeEditor))]
    public class RadioItemLngSettingsCollection : CollectionBase
    {
        internal object Owner
        {
            get;
            set;
        }
        public RadioItemLngSettingsCollection(object owner)
        {
            this.Owner = owner;
        }
        public RadioItemLngSettings GetItem(object obj)
        {

            foreach (RadioItemLngSettings s in List)
            {
                if (s.ItemValue.EqualsSafe(obj))
                    return s;
            }
            return null;
        }
        public void Add(RadioItemLngSettings item)
        {
            this.List.Add(item);
            OnCollectionCahnged();
        }
        public void AddRange(RadioItemLngSettings[] items)
        {
            foreach (RadioItemLngSettings item in items)
                this.List.Add(item);
        }
        public void Remove(RadioItemLngSettings item)
        {
            if (List.Contains(item))
                List.Remove(item);
            OnCollectionCahnged();
        }
        public new void RemoveAt(int i)
        {
            List.RemoveAt(i);
            OnCollectionCahnged();
        }

        public RadioItemLngSettings this[int i]
        {
            get { return List[i] as RadioItemLngSettings; }
        }

        protected override void OnClearComplete()
        {
            base.OnClearComplete();
            OnCollectionCahnged();
        }
        protected override void OnInsertComplete(int index, object value)
        {
            base.OnInsertComplete(index, value);
            OnCollectionCahnged();
        }
        protected override void OnRemoveComplete(int index, object value)
        {
            base.OnRemoveComplete(index, value);
            OnCollectionCahnged();
        }
        protected override void OnSetComplete(int index, object oldValue, object newValue)
        {
            base.OnSetComplete(index, oldValue, newValue);
            OnCollectionCahnged();
        }

        public event EventHandler CollectionCahnged;
        protected void OnCollectionCahnged()
        {
            if (CollectionCahnged != null)
                CollectionCahnged(this, EventArgs.Empty);
        }
    }
}
